<template>
	<view class="content">
		<image src="/static/bag/invite_img.png" mode="" class="imgs"></image>

		<ikun-qrcode width="300" height="300" unit="rpx" color="#000000" :data="url" >
			
		</ikun-qrcode>
		
		<button class="copyUrl" @click="copyUrl()">复制邀请链接</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo:null,
				url:null,
				user_token:null
			};
		},
		onLoad() {
			this.getUserInfo()
		},
		methods:{
			getUserInfo(){
				let that = this
				if(that.userInfo == null){
					uni.getStorage({
						key: 'user_token',
						success: function (res) {
							that.user_token = res.data
							that.checkUser()
						},
						fail: function(res){
							uni.showToast({
								title:'请先登录',
								icon:'none',
								success() {
									uni.redirectTo({
										url:'/pages/login/login',
									})
								}
							})
							
						}
					});
				}
			},
			checkUser(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/user/getUserInfo',
					method:'POST',
					header: {token:that.user_token},
					success(res) {
						if(res.data.code == 401){
							uni.removeStorage({
								key:'user_token',
								success() {
									uni.showToast({
										icon:'none',
										title:'登录已失效,请重新登录',
										success() {
											setTimeout(function(){
												uni.redirectTo({
													url:'/pages/login/login'
												})
											})
										}
									})
								}
							})
						}
						that.userInfo = res.data.data
						console.log(that.userInfo)
						that.url = getApp().globalData.baseUrl + "/#/pages/register/register?agent_id="+that.userInfo.id
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
			},
			copyUrl(){
				console.log(1231321)
				let url = getApp().globalData.baseUrl + "/#/pages/register/register?agent_id=" + this.userInfo.id
				navigator.clipboard.writeText(url).then(function(){
					uni.showToast({
						icon:'none',
						title:'复制成功'
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #f4f4f4;
	}
	.content{
		display: flex;
		justify-content: center;
		margin: 1rem 1rem;
		border-radius: 15px;
		flex-direction: column;
		align-items: center;
		background: white;
		padding: 1rem 1rem 1rem 1rem;
	}
	.imgs{
		width: 100%;
		border-radius: 15px;
	}
	.qrcode{
		margin-top: 1rem;
		width: 200px;
		height: 200px;
	}
	.copyUrl{
		margin-top: 1rem;
		width: 70%;
		color: #dd5154;
		border: 1px solid #dd5154;
		border-radius: 30px;
	}
</style>
